﻿using System;
using Firefly.Framework.ContentManagement;
using Firefly.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Firefly.Framework.Components
{
    public class TextCompositor : DrawableComponent
    {
        public string FontAsset { get; set; }
        public SpriteFont SpriteFont { get; set; }
        public float Scale { get; set; }
        public Vector2 Position { get; set; }
        public string Text { get; set; }
        public Color Color { get; set; }

        public TextCompositor(string fontAsset)
        {
            FontAsset = fontAsset;
            Scale = 1.0f;
            Position = Vector2.Zero;
            Color = Color.White;
        }

        #region IHasContent Members

        public override void LoadContent()
        {
            if (!string.IsNullOrEmpty(FontAsset))
            {
                SpriteFont = ContentService.Instance.Load<SpriteFont>(FontAsset);
            }
            else
            {
                throw new ArgumentNullException("The font asset cannot be null.");
            }
        }

        public override void UnloadContent()
        {
            if (!string.IsNullOrEmpty(FontAsset))
            {
                ContentService.Instance.Release(FontAsset);
            }
        }

        #endregion

        #region IDrawable Members

        public override void Draw(GameTime gameTime)
        {
            GraphicsService.Instance.SpriteBatch.Begin();
            GraphicsService.Instance.SpriteBatch.DrawString(SpriteFont, Text, Position, Color);
            GraphicsService.Instance.SpriteBatch.End();
        }

        #endregion

        #region IDisposable Members

        ~TextCompositor()
        {
            Dispose(false);
        }

        #endregion
    }
}
